package com.example.sunsg.okhttptest.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.Manager.FrescoManager;
import com.example.sunsg.okhttptest.bean.SpotEditItem;
import com.example.sunsg.okhttptest.bean.SpotEditItemDescription;
import com.example.sunsg.okhttptest.bean.SpotEditItemImage;
import com.example.sunsg.okhttptest.bean.SpotEditItemOtherInfo;
import com.example.sunsg.okhttptest.bean.SpotEditItemPlue;
import com.example.sunsg.okhttptest.util.DisplayUtils;
import com.example.sunsg.okhttptest.util.Util;
import com.example.sunsg.okhttptest.viewholder.SpotEditItemDescriptionHolder;
import com.example.sunsg.okhttptest.viewholder.SpotEditItemImageHolder;
import com.example.sunsg.okhttptest.viewholder.SpotEditItemOtherInfoHolder;
import com.example.sunsg.okhttptest.viewholder.SpotEditItemPlueHolder;

import java.util.List;

/**
 * Created by sunsg on 16/3/9.
 */
public class SpotEditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<SpotEditItem> mItems;
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private int mImageWidth;
//    private MyTextWatch mTextWatch;
    public SpotEditAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mImageWidth = DisplayUtils.getScreenWidth(context) - Util.dp2px(context, 20);//20 左右俩边的总距离
//        mTextWatch = new MyTextWatch();
    }

    public void setItems(List<SpotEditItem> items) {
        this.mItems = items;
        notifyDataSetChanged();
    }

    public List<SpotEditItem> getItems() {
        return mItems;
    }

    public void updateItemDescription(int position,String description){
        SpotEditItem item = mItems.get(position);
        if(item.getType() == SpotEditItem.TYPE_IMAGE){
            SpotEditItemImage imageItem = (SpotEditItemImage) item.getItem();
            imageItem.setDescription(description.trim());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case SpotEditItem.TYPE_DESCRIPTION:
                view = mLayoutInflater.inflate(R.layout.item_activity_spot_edit_description, parent, false);
                holder = new SpotEditItemDescriptionHolder(view);
                break;
            case SpotEditItem.TYPE_PLUE:
                view = mLayoutInflater.inflate(R.layout.item_activity_spot_edit_plue, parent, false);
                holder = new SpotEditItemPlueHolder(view);
                break;
            case SpotEditItem.TYPE_IMAGE:
                view = mLayoutInflater.inflate(R.layout.item_activity_spot_edit_image, parent, false);
                SpotEditItemImageHolder imageHolder = new SpotEditItemImageHolder(view);
                imageHolder.eTthuzhu.setTag(new MyTextWatch());
                holder = imageHolder;
                break;
            case SpotEditItem.TYPE_OTHER_INFO:
                view = mLayoutInflater.inflate(R.layout.item_activity_spot_edit_other_info, parent, false);
                holder = new SpotEditItemOtherInfoHolder(view);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int type = getItemViewType(position);
        SpotEditItem item = mItems.get(position);
        switch (type) {
            case SpotEditItem.TYPE_DESCRIPTION:
                SpotEditItemDescriptionHolder descriptionHolder = (SpotEditItemDescriptionHolder) holder;
                SpotEditItemDescription descriptionItem = (SpotEditItemDescription) item.getItem();
                break;
            case SpotEditItem.TYPE_PLUE:
                SpotEditItemPlueHolder plueHolder = (SpotEditItemPlueHolder) holder;
                SpotEditItemPlue plueItem = (SpotEditItemPlue) item.getItem();
                break;
            case SpotEditItem.TYPE_IMAGE:
                SpotEditItemImageHolder imageHolder = (SpotEditItemImageHolder) holder;
                SpotEditItemImage imageItem = (SpotEditItemImage) item.getItem();

                //加载图片
                int realHeight = 0;
                if(imageItem.getImage().getImage().mRotation == 90 || imageItem.getImage().getImage().mRotation == 270){
                    //图片横向
                    realHeight = (int) (1.0 * mImageWidth * imageItem.getImage().getImage().getWidth() / imageItem.getImage().getImage().getHeight());
                }else{
                    //正常图片
                    realHeight = (int) (1.0 * mImageWidth * imageItem.getImage().getImage().getHeight() / imageItem.getImage().getImage().getWidth());
                }

                imageHolder.mSimpleDraweeView.getLayoutParams().height = realHeight;
                imageHolder.mSimpleDraweeView.getLayoutParams().width = mImageWidth;
                FrescoManager.loadFilePath(imageItem.getImage().getImage().getDataPath()).autoRotate(true).resize(mImageWidth, realHeight)
                        .placeImageHolder(R.drawable.place_holder_big).builder().into(imageHolder.mSimpleDraweeView);
                //文本输入
                MyTextWatch watch = (MyTextWatch) imageHolder.eTthuzhu.getTag();
                imageHolder.eTthuzhu.removeTextChangedListener(watch);
                imageHolder.eTthuzhu.setText(imageItem.getDescription());
//                watch.setPosition(position);
                watch.setItem(imageItem);
                imageHolder.eTthuzhu.addTextChangedListener(watch);
                imageHolder.eTthuzhu.setTag(watch);
                break;
            case SpotEditItem.TYPE_OTHER_INFO:
                SpotEditItemOtherInfoHolder otherInfoHolder = (SpotEditItemOtherInfoHolder) holder;
                SpotEditItemOtherInfo otherInfo = (SpotEditItemOtherInfo) item.getItem();
                otherInfoHolder.tvDate.setText(otherInfo.getDate());
                break;
        }
    }

    @Override
    public int getItemCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        return mItems.get(position).getType();
    }

    class MyTextWatch implements TextWatcher{
        SpotEditItemImage imageItem;
        int position;
        public MyTextWatch() {

        }

        public void setItem(SpotEditItemImage item){
            imageItem = item;
        }

        public void setPosition(int position){
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
//            updateItemDescription(position,s.toString());
            imageItem.setDescription(s.toString().trim());
        }
    }
}
